home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / init.d / hdparm < prev    next >
Encoding:
Text File  |  2013-01-10  |  10.3 KB  |  407 lines

  1. #!/bin/sh
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides:          hdparm
  5. # Required-Start:    mountdevsubfs
  6. # Required-Stop:
  7. # Should-Start:      udev
  8. # Default-Start:
  9. # Default-Stop:      S
  10. # Short-Description: Tune IDE hard disks
  11. ### END INIT INFO
  12.  
  13. set -e
  14.  
  15. . /lib/lsb/init-functions
  16.  
  17. # Defaults for configuration variables.
  18. RAID_WORKAROUND=no
  19.  
  20. # Source the defaults file.
  21. [ -e /etc/default/hdparm ] && . /etc/default/hdparm
  22.  
  23. raid_speed_limit_min=
  24. raid_speed_limit_max=
  25.  
  26. case "$0" in
  27.   *hdparm)
  28.     FIRST=yes
  29.     ;;
  30.   *)
  31.     FIRST=no
  32.     ;;
  33. esac
  34.  
  35. case "$1" in
  36.   start|restart|reload|force-reload)
  37.   UDEV=no
  38.   ;;
  39.   hotplug)
  40.   UDEV=yes
  41.   [ "$DEVNAME" ] || exit 1
  42.   ;;
  43.   stop)
  44.   exit 0
  45.   ;;
  46.   *)
  47.   log_failure_msg "Usage: $0 {stop|start|restart|reload|force-reload|hotplug}" >&2
  48.   exit 3
  49.   ;;
  50. esac
  51.  
  52. if [ "$FORCE_RUN" != 'yes' ]; then
  53.   if [ -e /proc/cmdline ]; then #linux only - future proofing against BSD and Hurd :)
  54.     if grep -wq "nohdparm" /proc/cmdline ; then
  55.       log_warning_msg "Skipping setup of disc parameters."
  56.       exit 0
  57.     fi
  58.   fi
  59.  
  60.   raidstat=OK
  61.   if [ -e /proc/mdstat ]; then
  62.     if egrep -iq "resync|repair|recover|check" /proc/mdstat; then
  63.       raidstat=RESYNC
  64.     fi
  65.   elif [ -e /proc/rd/status ]; then
  66.     raidstat=`cat /proc/rd/status`
  67.   fi
  68.  
  69.   if ! [ "$raidstat" = 'OK' ] && [ "$RAID_WORKAROUND" != "yes" ]; then
  70.     log_failure_msg "RAID status not OK.  Exiting."
  71.     exit 0
  72.   fi
  73. fi
  74.  
  75. slow_down_raid_sync()
  76. {
  77.   if [ -f /proc/sys/dev/raid/speed_limit_min ]; then
  78.     raid_speed_limit_min=`cat /proc/sys/dev/raid/speed_limit_min`
  79.     echo 0 >/proc/sys/dev/raid/speed_limit_min
  80.   fi
  81.   if [ -f /proc/sys/dev/raid/speed_limit_max ]; then
  82.     raid_speed_limit_max=`cat /proc/sys/dev/raid/speed_limit_max`
  83.     echo 0 >/proc/sys/dev/raid/speed_limit_max
  84.   fi
  85.   sleep 2
  86.   trap undo_slow_down_raid_sync EXIT
  87. }
  88.  
  89. undo_slow_down_raid_sync()
  90. {
  91.   if [ -f /proc/sys/dev/raid/speed_limit_min ] && [ "x$raid_speed_limit_min" != "x" ]; then
  92.     echo $raid_speed_limit_min >/proc/sys/dev/raid/speed_limit_min
  93.   fi
  94.   if [ -f /proc/sys/dev/raid/speed_limit_max ] && [ "x$raid_speed_limit_max" != "x" ]; then
  95.     echo $raid_speed_limit_max >/proc/sys/dev/raid/speed_limit_max
  96.   fi
  97. }
  98.  
  99. set_option()
  100. {
  101.   if test -n "$DISC"; then
  102.     NEW_OPT=
  103.     for i in $OPTIONS; do
  104.       if test x${i%${i#??}} != x${1%${1#??}}; then
  105.         NEW_OPT="$NEW_OPT $i"
  106.       else
  107.         NEW_OPT=${NEW_OPT%-q}
  108.       fi
  109.     done
  110.     OPTIONS="$NEW_OPT $OPT_QUIET $1"
  111.   else
  112.     NEW_DEF=
  113.     for i in $DEFAULT; do
  114.       if test x${i%${i#??}} != x${1%${1#??}}; then
  115.         NEW_DEF="$NEW_DEF $i"
  116.       else
  117.         NEW_DEF=${NEW_DEF%-q}
  118.       fi
  119.     done
  120.     DEFAULT="$NEW_DEF $DEF_QUIET $1"
  121.   fi
  122. }
  123.  
  124. eval_value()
  125. {
  126.   case $1 in
  127.     off|0)
  128.       set_option "$2"0
  129.        ;;
  130.     on|1)
  131.       set_option "$2"1
  132.       ;;
  133.     *)
  134.       log_failure_msg "Unknown Value for $2: $1"
  135.       exit 1
  136.       ;;
  137.   esac
  138. }
  139.  
  140. WAS_RUN=0
  141.  
  142. # Turn off RAID synchronisation if needed and asked for.
  143. if [ "$raidstat" != 'OK' ] && [ "$RAID_WORKAROUND" = "yes" ]; then
  144.   slow_down_raid_sync
  145. fi
  146.  
  147. # Get blocks as far as the drive's write cache.
  148. /bin/sync
  149.  
  150. [ "$UDEV" = 'yes' ] || log_daemon_msg "Setting parameters of disc"
  151.  
  152. DISC=
  153. DEFAULT=
  154. OPTIONS=
  155. DEF_QUIET=
  156. OPT_QUIET=
  157.  
  158. egrep -v '^[[:space:]]*(#|$)' /etc/hdparm.conf | 
  159. {
  160.   while read KEY SEP VALUE; do
  161.     if [ "$NEXT_LINE" != 'go' ]; then
  162.       case $SEP in
  163.         '{')
  164.           case $KEY in
  165.             command_line)
  166.               NEXT_LINE=go
  167.               unset DISC
  168.               unset OPTIONS
  169.               unset OPT_QUIET
  170.               if [ "$UDEV" = 'yes' ]; then
  171.                 IN_BLOCK=0
  172.               fi
  173.               ;;
  174.             *)
  175.               if [ -h "$KEY" ]; then
  176.                 DISC=$(readlink -m "$KEY")
  177.               else
  178.                 DISC=$KEY
  179.               fi
  180.               OPTIONS=$DEFAULT
  181.               OPT_QUIET=$DEF_QUIET
  182.               WAS_RUN=0
  183.               if [ "$UDEV" = 'yes' ]; then
  184.                 if [ "$DISC" = "$DEVNAME" ]; then
  185.                   IN_BLOCK=1
  186.                 else
  187.                   IN_BLOCK=0
  188.                 fi
  189.               fi
  190.               ;;
  191.           esac
  192.           ;;
  193.         =)
  194.           case $KEY in
  195.             read_ahead_sect) 
  196.               set_option -a$VALUE
  197.               ;;
  198.             lookahead) 
  199.               eval_value $VALUE -A
  200.               ;;
  201.             bus) 
  202.               eval_value $VALUE -b
  203.               ;;
  204.             apm) 
  205.               set_option -B$VALUE
  206.               ;;
  207.             io32_support) 
  208.               set_option -c$VALUE
  209.               ;;
  210.             dma) 
  211.               eval_value $VALUE -d
  212.               ;;
  213.             defect_mana) 
  214.               eval_value $VALUE -D
  215.               ;;
  216.             cd_speed) 
  217.               set_option -E$VALUE
  218.               ;;
  219.             mult_sect_io) 
  220.               set_option -m$VALUE
  221.               ;;
  222.             prefetch_sect) 
  223.               set_option -P$VALUE
  224.               ;;
  225.             read_only) 
  226.               eval_value $VALUE -r
  227.               ;;
  228.             spindown_time) 
  229.               case "$VALUE" in
  230.                 *[hms])
  231.                   case "$VALUE" in
  232.                     *h)
  233.                     time=$((${VALUE%h} * 3600))
  234.                     ;;
  235.                     *m)
  236.                     time=$((${VALUE%m} * 60))
  237.                     ;;
  238.                     *s)
  239.                     time=${VALUE%s}
  240.                     ;;
  241.                   esac
  242.                   if [ $time -lt 1260 ]; then # up to 21 minutes
  243.                       new_VALUE=$(($time / 5))
  244.                       if [ $new_VALUE -gt 240 ]; then
  245.                           new_VALUE=240
  246.                       fi
  247.                       if [ $(($new_VALUE * 5)) -ne $time ]; then
  248.                           log_warning_msg "$VALUE not possible, using $(($new_VALUE * 5)) seconds"
  249.                       fi
  250.                       VALUE=$new_VALUE
  251.                   elif [ $time -lt 1800 ]; then
  252.                       if [ $time -ne 1260 ]; then
  253.                           log_warning_msg "$VALUE not possible, using 21 minutes"
  254.                       fi
  255.                       VALUE=252
  256.                   else
  257.                       new_time=$(($time / 1800))
  258.                       if [ $new_time -gt 11 ]; then
  259.                           new_time=11
  260.                       fi
  261.                       if [ $((new_time * 1800)) -ne $time ]; then
  262.                           log_warning_msg "$VALUE not possible, using $(($new_time * 30)) minutes"
  263.                       fi
  264.                       VALUE=$((new_time + 240))
  265.                   fi
  266.                   ;;
  267.               esac
  268.               set_option -S$VALUE
  269.               ;;
  270.             poweron_standby) 
  271.               eval_value $VALUE -s
  272.               ;;
  273.             interrupt_unmask) 
  274.               eval_value $VALUE -u
  275.               ;;
  276.             write_cache) 
  277.               eval_value $VALUE -W
  278.               ;;
  279.             transfer_mode) 
  280.               set_option -X$VALUE
  281.               ;;
  282.             acoustic_management)
  283.               set_option -M$VALUE
  284.               ;;
  285.             keep_settings_over_reset)
  286.               eval_value $VALUE -k
  287.              ;;
  288.             keep_features_over_reset)
  289.               eval_value $VALUE -K
  290.              ;;
  291.             chipset_pio_mode)
  292.               set_option -p$VALUE
  293.              ;;
  294.             security_unlock)
  295.               set_option --security-unlock $VALUE
  296.              ;;
  297.             security_pass)
  298.               set_option --security-set-pass $VALUE
  299.              ;;
  300.             security_disable)
  301.               set_option --security-disable $VALUE
  302.              ;;
  303.             user-master)
  304.               set_option --user-master $VALUE
  305.               ;;
  306.             security_mode)
  307.               set_option --security-mode $VALUE
  308.              ;;
  309.             ROOTFS)
  310.               ROOTFS=$VALUE
  311.              ;; 
  312.             *)
  313.               log_failure_msg "Unknown option $KEY"
  314.               exit 1
  315.               ;;
  316.           esac
  317.           ;;
  318.         "")
  319.           case $KEY in
  320.             '}')
  321.               if [ -z "$DISC" ] && [ "$WAS_RUN" != '1' ]; then
  322.                 log_failure_msg "No disk enabled. Exiting"
  323.                 exit 1
  324.               fi
  325.               if [ -n "$OPTIONS" ] && [ -b "$DISC" ]; then
  326.                 if [ -n "$ROOTFS" ]; then
  327.                   if [ "$FIRST" = 'yes' ] && [ "$DISC" != "$ROOTFS" ]; then
  328.                     continue
  329.                   fi
  330.                   if [ "$FIRST" = 'no' ] && [ "$DISC" = "$ROOTFS" ]; then
  331.                     continue
  332.                   fi
  333.                 fi
  334.                 ret=0
  335.                 if [ "$UDEV" = 'yes' ] && [ "$IN_BLOCK" = 1 ]; then
  336.                   # Flush the drive's internal write cache to the disk.
  337.                   /sbin/hdparm -q -f $DISC 2>/dev/null || ret=$?
  338.                   /sbin/hdparm $OPTIONS $DISC 2>/dev/null || ret=$?
  339.                   if [ "$VERBOSE" = 'yes' ]; then
  340.                     log_progress_msg " $DISC"
  341.                     log_end_msg $ret || true
  342.                   fi
  343.                 elif [ "$UDEV" = 'no' ]; then
  344.                   /sbin/hdparm -q -f $DISC 2>/dev/null || ret=$?
  345.                   /sbin/hdparm $OPTIONS $DISC 2>/dev/null || ret=$?
  346.                   WAS_RUN=1
  347.                   log_progress_msg " $DISC"
  348.                   log_end_msg $ret || true
  349.                 fi
  350.               fi       
  351.               ;;
  352.             quiet)
  353.               if [ -n "$DISC" ]; then
  354.                 OPT_QUIET=-q
  355.               else
  356.                 DEF_QUIET=-q
  357.               fi
  358.               ;;
  359.             standby) 
  360.               set_option -y
  361.               ;;
  362.             sleep) 
  363.               set_option -Y
  364.               ;;
  365.             disable_seagate) 
  366.               set_option -Z
  367.               ;;
  368.             security_freeze) 
  369.               set_option --security-freeze
  370.               ;;
  371.             *)
  372.               log_failure_msg "unknown option $KEY"
  373.               exit 1
  374.               ;;
  375.           esac
  376.           ;;
  377.        *)
  378.          log_failure_msg "unknown separator $SEP"
  379.          exit 1
  380.          ;;
  381.       esac
  382.     else
  383.       $KEY $SEP $VALUE
  384.       NEXT_LINE=no-go
  385.       WAS_RUN=1
  386.     fi
  387.   done
  388.  
  389.   if [ -n "$harddisks" ] && [ -n "$hdparm_opts" ] && [ "$UDEV" = 'no' ]; then
  390.     ret=0
  391.     for drive in $harddisks; do
  392.       WAS_RUN=1
  393.       log_progress_msg "$drive "
  394.       /sbin/hdparm -q -f $drive 2>/dev/null|| ret=$?
  395.       /sbin/hdparm -q $hdparm_opts -q $drive 2>/dev/null|| ret=$?
  396.     done
  397.     log_end_msg $ret || true
  398.   fi
  399.   
  400.   if [ "$UDEV" = 'no' -a "$WAS_RUN" = 0 ]; then
  401.     log_progress_msg "(none)"
  402.     log_end_msg 0
  403.   fi
  404. }
  405.  
  406. exit 0
  407.